import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
function setLocaStorage(state){
  window.localStorage.setItem('user_token',JSON.stringify({...state}))
}
export default new Vuex.Store({
  state: {
    startData: '请选择地址',
    endData: '请选择终点',
    userInfo: {
      name: '',
      head_photo: '',//默认头像
      phone: '',
      sex: "",
      pay_type: {
        wx: '',
        zfb: ''
      }
    },
    location: {
    },
    driver_msg:{}
  },
  mutations: {
    setDriver_msg(state, obj) {
      console.log(obj);
      state.driver_msg = obj;
    },
    setLocation(state, obj) {
      console.log(obj);
      state.location = obj
    },
    setUserPayTypeWX(state, str) {
      state.userInfo.pay_type.wx = str
      setLocaStorage(state.userInfo)
    },
    setUserPayTypeZFB(state, str) {
      state.userInfo.pay_type.zfb = str
      setLocaStorage(state.userInfo)
    },
    setUserName(state, str) {
      state.userInfo.name = str;
      setLocaStorage(state.userInfo)
    },
    setUserSex(state, str) {
      state.userInfo.sex = str;
      setLocaStorage(state.userInfo)
    },
    setUserHeadPhoto(state, str) {
      state.userInfo.head_photo = str;
      setLocaStorage(state.userInfo)
    },
    setUserPhone(state, num) {
      state.userInfo.phone = Number(num);
      setLocaStorage(state.userInfo)
    },
    setStartData(state, txt) {
      state.startData = txt
    },
    setEndData(state, txt) {
      state.endData = txt;
    }
  },
  getters: {
    filterPhone: state => {
      let { phone } = state.userInfo;
      // console.log(phone);
      let str = phone.toString();
      if (str.length !== 11) {
        console.log('sssadfasdfasdfasdfas');
        return '手机号有误，重新设置'
      }
      // retrun phone.toString().substring(phone.toString().length-4)
      return str.substring(0, 3) + '****' + str.substring(str.length - 4);
    }
  },
  actions: {
  },
  modules: {
  }
})
